package org.example.analysis.service.dataobj;

import com.github.javaparser.ast.expr.AnnotationExpr;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.example.analysis.model.ClassComplexity;
import org.example.analysis.model.entity.JavaClassInfo;
import org.example.analysis.model.entity.JavaFieldInfo;
import org.example.analysis.model.entity.JavaMethodInfo;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 类信息数据对象
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ClassDataObject extends BaseDataObject {
    /**
     * 项目名称
     */
    private String projectName;
    
    /**
     * 类名
     */
    private String className;
    
    /**
     * 包名
     */
    private String packageName;
    
    /**
     * 类型（枚举：INTERFACE, ABSTRACT, NORMAL）
     */
    private String classType;
    
    /**
     * 父类
     */
    private String superClass;
    
    /**
     * 类注释
     */
    private String comment;
    
    /**
     * 代码行数
     */
    private Integer codeLines;
    
    /**
     * 实现的接口列表
     */
    private List<String> interfaces = new ArrayList<>();
    
    /**
     * 类的字段列表
     */
    private List<FieldDataObject> fields = new ArrayList<>();
    
    /**
     * 类的方法列表
     */
    private List<MethodDataObject> methods = new ArrayList<>();
    private ClassComplexity ClassComplexity = new ClassComplexity();
    private Integer complexityScore;
    
    /**
     * 类的注解列表
     */
    private List<String> annotations = new ArrayList<>();
    
    /**
     * 类的修饰符列表
     */
    private List<String> modifiers = new ArrayList<>();
    
    /**
     * 是否是接口
     */
    private boolean isInterface;
    
    /**
     * 是否是抽象类
     */
    private boolean isAbstract;
    
    /**
     * 源代码文件路径
     */
    private String sourceFilePath;
    
    /**
     * 内部类列表
     */
    private List<ClassDataObject> innerClasses = new ArrayList<>();

    public String fullQualifiedName;
    private String classComment;
    //private List<AnnotationExpr> annotationsExpr= Lists.newArrayList();

    /**
     * 最后修改人
     */
    private String lastModifiedBy;

    public void addAnnotaion(AnnotationExpr annotation) {
        //annotationsExpr.add(annotation);
        annotations.add("@" + annotation.getNameAsString());
    }

    /**
     * 判断是否是测试类
     */
    public boolean isTestClass() {
        // 检查类名是否包含Test
        if (className != null && (className.endsWith("Test") || className.endsWith("Tests"))) {
            return true;
        }
        // 检查是否有测试相关注解
        if (annotations != null) {
            for (String annotation : annotations) {
                if (annotation.contains("Test") || annotation.contains("RunWith")) {
                    return true;
                }
            }
        }
        return false;
    }
    public JavaClassInfo toJavaClassInfo() {
        JavaClassInfo classInfo = new JavaClassInfo();
        // 基本信息
        classInfo.setProjectName(projectName)
                .setClassName(className)
                .setPackageName(packageName)
                .setClassType(classType)
                .setSuperClass(superClass)
                .setFullQualifiedName(fullQualifiedName)
                .setClassComment(classComment)
                .setComplexityScore(complexityScore);

        // 类型标记
        classInfo.setInterface(isInterface)
                .setAbstract(isAbstract)
                .setInnerClass(className != null && className.contains("$"))
                .setTestClass(isTestClass());

        // 统计信息
        classInfo.setCodeLines(codeLines != null ? codeLines : 0);

        // 列表信息
        classInfo.setModifiers(modifiers != null ? new ArrayList<>(modifiers) : new ArrayList<>())
                .setInterfaces(interfaces != null ? new ArrayList<>(interfaces) : new ArrayList<>())
                .setAnnotations(annotations != null ? new ArrayList<>(annotations) : new ArrayList<>());

        // 时间和更新信息
        classInfo.setLastModifiedTime(getLastModifiedTime())
                .setLastModifiedBy(lastModifiedBy)
                .setCreatedAt(getCreatedAt())
                .setRefreshable(getRefreshable());
        return classInfo;
    }

}